/*******************************************************************************
 * Dreampalace
 *******************************************************************************
 * 
 * Dreampalace is a special dreamdiary which helps dreamers to understand their
 * dreams. You can find also many features in Dreampalace to learn and hold the
 * lucid state in your dreams.
 * 
 * *****************************************************************************
 *
 * Program	   : Dreampalace
 * Autor           : PRo (Peter Rogge)
 * Email           : rogge-peter@gmx.de
 * Homepage        : http://code.google.com/p/dreampalace
 * License	   : GNU General Public License 3.0
 * 
 *******************************************************************************
 * 
 * This file is part of the project Dreampalace. Dreampalace is free software:
 * you can redistribute it and/or modify it under the terms of the GNU General 
 * Public License as published by the Free Software Foundation in version 3 of
 * the License only.
 * 
 * Dreampalace is distributed in the hope that it will be useful, but WITHOUT 
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU General Public License along with
 * Dreampalace. If not, see <http://www.gnu.org/licenses/>.
 * 
 * The Dreampalace project designates this particular file as subject to the
 * "Classpath" exception as provided by the Dreampalace project in the GPL 
 * Version 3 section of the License file that accompanied this code.
 * 
 *******************************************************************************
 */
package de.pro.dreampalace.database;

import de.pro.dreampalace.database.api.IConnectionManager;
import de.pro.dreampalace.database.api.IDatabaseManager;
import java.sql.*;
import org.openide.util.Lookup;
import org.openide.util.lookup.ServiceProvider;

/**
 *
 * @author Peter Rogge (PRo) <rogge-peter@gmx.de>
 */
@ServiceProvider(service=IDatabaseManager.class)
public class DatabaseManager implements IDatabaseManager {

    @Override
    public boolean checkTable(String table) {
        ResultSet result = null;
        Statement stmt = null;
        
        try {
            stmt = this.getConnection().createStatement();
            result = stmt.executeQuery("SELECT COUNT(*) FROM " + table); // NOI18N
        } catch (SQLException ex) {
            return Boolean.FALSE;
        } finally {
            try {
                if (result != null && !result.isClosed()) { result.close(); }
                if (stmt != null && !stmt.isClosed()) { stmt.close(); }
            } catch (SQLException ex) { }
        }

        return Boolean.TRUE;
    }

    @Override
    public void createTable(String sql) throws SQLException {
        Statement statement = null;
        try {
            statement = this.getConnection().createStatement();
            statement.executeUpdate(sql);
        } finally {
            try {
                statement.close();
            } catch (SQLException ex) { }
        }
        
    }

    @Override
    public void fillTable(String[] values) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public Connection getConnection() {
        final IConnectionManager cm = Lookup.getDefault().lookup(IConnectionManager.class);

        return cm.getConnection();
    }
}
